package huizhi.exercise;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class FileTreeExerciese {
    public static void main(String[] args) {
        fileTreeView("C:/Users/Administrator/Desktop/后端");

    }
    private static int a = 0;

    /**
     * 写一个方法，传入一个路径，打印路径树
     */
    private static void fileTreeView(String url) {
        File file = new File(url);
        fileTreeView2(file.getAbsolutePath(),0);
    }
    private static void fileTreeView2(String url,int b) {
        File file = new File(url);
        File [] files = file.listFiles();
        for (File file1 : files) {
            if (file1.isDirectory()){
                for (int i = 0; i < b; i++) {
                    System.out.print(" ");
                }
                System.out.println("^"+file1.getName());
                fileTreeView2(file1.getAbsolutePath(),b+2);
            }
            List<String> strings = new ArrayList<>();
            String a = "";
            if (file1.isFile()){

                for (int i = 0; i < b; i++) {
//                    System.out.print(" ");
                    a += " ";
                }
                a += file1.getName();
//                System.out.println(file1.getName());
                strings.add(a);
            }
            for (String string : strings) {
                System.out.println(string);
            }

        }
    }
}
